#include <Windows.h>
#include <gdiplus.h>

#include "Settings.h"
#include "CassiopeiaMain.h"
#include "Translation.h"


///Copyright (c) 2009-2010 T. Klepzig 

//Enable Visual Styles
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' \
version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

using namespace Gdiplus;


LRESULT CALLBACK WndProc(LPVOID sender, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{	
	//always active, never inactive
	if (uMsg == WM_NCACTIVATE && !wParam)
		return FALSE;

	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

LRESULT CALLBACK WndProc2(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{	
	//always inactive when activating with mouse
	if (uMsg == WM_MOUSEACTIVATE)
		return MA_NOACTIVATE;

	//always inactive when activating by clicking the non-client area
	if (uMsg == WM_NCACTIVATE && wParam)
		return FALSE;
	
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}


int APIENTRY _tWinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPTSTR lpCmdLine,
	int nShowCmd)
{
	#pragma region Show Debug Console

	#if _DEBUG
		AllocConsole();
		AttachConsole(GetCurrentProcessId());
		freopen_s((FILE**)stdout,"CON", "w", stdout);
	#endif

	#pragma endregion	

	//get all cd-rom drives
	/*DWORD logicalDrives = GetLogicalDrives();

	for (int drive = 0; drive < 32; drive++)
	{
		if (logicalDrives & (1 << drive))
		{
			TCHAR volumeLetter = drive + 'A';
			TCHAR driveRoot[] = {volumeLetter, ':', '\\', '\0'};

			if (GetDriveType(driveRoot) == DRIVE_CDROM)
				_tprintf(TEXT("%s\n"), driveRoot);
		}
	}
	*/

	//disable snap animation for hWnd
	/*HWND h = FindWindow(TEXT("Notepad"),0);
	BOOL b=TRUE;
	DwmSetWindowAttribute(h,
						DWMWA_TRANSITIONS_FORCEDISABLED,
						&b,
						sizeof(DWMWA_TRANSITIONS_FORCEDISABLED));*/


	#pragma region Allow only one instance of WinTools

	HANDLE hMutex = CreateMutex(0,FALSE,TEXT("{40035345-53E7-43b2-AEFB-D218A3E0B294}"));
	
	if (hMutex && GetLastError() == ERROR_ALREADY_EXISTS)
	{
		ReleaseMutex(hMutex);
		return 0;
	}
	
	ReleaseMutex(hMutex);

	#pragma endregion	

	#pragma region initialize gdi+

	ULONG_PTR gdiplusToken;  
	GdiplusStartupInput gdiplusStartupInput;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, 0);
	
	#pragma endregion	

	#pragma region manipulate working directory, so that it is always the same directory as the executable

	TCHAR path[_MAX_PATH]={0};
	TCHAR drive[_MAX_DRIVE]={0};
	TCHAR dir[_MAX_DIR]={0};
	TCHAR fileName[_MAX_FNAME]={0};
	TCHAR ext[_MAX_EXT]={0};

	GetModuleFileName(0, path, _MAX_PATH);

	_tsplitpath(path,drive,dir,fileName,ext);
	_tmakepath(path,drive, dir, 0,0);

	SetCurrentDirectory(path);

	#pragma endregion

	#pragma region evaluate lpCmdLine and set language

	TCHAR *t = _tcstok( lpCmdLine, TEXT("/lang ") ) ;
	if (t)
	{
		if (!_tcscmp(t, TEXT("reset")) || !_tcscmp(t, TEXT("default")))
		{
			Settings settings;
			Settings::Data data={0};

			settings.Load(&data);
			_tcscpy(data.lang, TEXT("default"));
			settings.Save(data);
		}
		else
		{
			if (!Translation::ExistsLanguageFile(t))
				Translation::WriteLanguageFile(t, t);

			Translation::SetLanguage(t);

			Settings settings;
			Settings::Data data={0};

			settings.Load(&data);
			_tcscpy(data.lang, t);
			settings.Save(data);
		}
	}
	else
	{
		Settings settings;
		Settings::Data data={0};
		settings.Load(&data);

		if (!Translation::ExistsLanguageFile(data.lang))
			Translation::WriteLanguageFile(data.lang, data.lang);

		Translation::SetLanguage(data.lang);
	}

	#pragma endregion

	CassiopeiaMain *cm = CassiopeiaMain::CreateInstance(hInstance);	

	MSG msg;
	while(GetMessage(&msg,0,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	delete cm;

	#pragma region shutdown gdi+

	GdiplusShutdown(gdiplusToken);

	#pragma endregion	

	return msg.wParam;
}
